package com.fimuz.mqmeet.api.building.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fimuz.mqmeet.api.building.dao.BuildingDao;
import com.fimuz.mqmeet.api.building.service.BuildingService;
import com.fimuz.mqmeet.api.building.service.model.CreateOrUpdateSpModel;
import com.fimuz.mqmeet.api.building.service.model.CreateOrUpdateSrModel;
import com.fimuz.mqmeet.api.building.service.model.DeleteSpModel;
import com.fimuz.mqmeet.api.building.service.model.DeleteSrModel;
import com.fimuz.mqmeet.api.building.service.model.GetSpModel;
import com.fimuz.mqmeet.api.building.service.model.GetSrModel;
import com.fimuz.mqmeet.api.building.service.model.ListSpModel;
import com.fimuz.mqmeet.api.building.service.model.ListSrModel;
import com.fimuz.mqmeet.helper.mybatis.dao.HxzBuildingMapper;
import com.fimuz.mqmeet.helper.mybatis.model.HxzBuilding;
import com.fosun.spr.core.base.CoBaseService;
import com.fosun.spr.core.utils.ExtAsserts;
import com.fosun.spr.core.utils.SprDaoUtils;

@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
@Service
public class BuildingServiceImpl extends CoBaseService implements
		BuildingService {
	@Resource
	private BuildingDao buildingDao;
	@Resource
	private HxzBuildingMapper hxzBuildingMapper;

	@Override
	public ListSrModel list(ListSpModel spModel) {
		ListSrModel srModel = new ListSrModel();
		srModel.setDatas(buildingDao.list(spModel));
		//srModel.setTotalCount(buildingDao.listCount(spModel));
		return srModel;
	}

	@Override
	public CreateOrUpdateSrModel createOrUpdate(CreateOrUpdateSpModel spModel) {
		ExtAsserts.notNull(spModel.getBuildingName(), "buildName");
		CreateOrUpdateSrModel srModel = new CreateOrUpdateSrModel();
		if (StringUtils.isBlank(spModel.getId())) {
			HxzBuilding record = new HxzBuilding();
			record.setTeamId(spModel.getSessionTeamId());
			record.setBuildingName(spModel.getBuildingName());
			SprDaoUtils.insert(hxzBuildingMapper, record);
			srModel.setId(record.getId());
		} else {
			HxzBuilding record = (HxzBuilding) SprDaoUtils.getById(
					hxzBuildingMapper, spModel.getId());
			record.setBuildingName(spModel.getBuildingName());
			SprDaoUtils.updateByPrimaryKey(hxzBuildingMapper, record);
		}
		return srModel;
	}

	@Override
	public DeleteSrModel delete(DeleteSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(), "id");
		DeleteSrModel srModel = new DeleteSrModel();
		SprDaoUtils.delete(hxzBuildingMapper, spModel.getId());
		return srModel;
	}

	@Override
	public GetSrModel get(GetSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(), "id");
		GetSrModel srModel = new GetSrModel();
		HxzBuilding record = (HxzBuilding) SprDaoUtils.getById(
				hxzBuildingMapper, spModel.getId());
		cglibCopyProperties(record, srModel, true);
		return srModel; 
	}

}
